package com.cloudspace.jindun.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class FileUtil {
	public static final String FILE_PATH=".jindun";

    private static int cnt=0;

    public static void checkDir(String paramString) {
        File localFile = new File(getDocumentPath() + "/" + paramString);
        if (!localFile.exists()){
            localFile.mkdirs();
        }
    }

    public static void checkFile(String paramString) {
        File localFile = new File(paramString);
        try {
            if (!localFile.exists())
                localFile.createNewFile();

        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
    }

    public static void deleteFile(File paramFile) {
        if (paramFile.exists()) {
            if (paramFile.isFile()) {
                paramFile.delete();
            }
            if (paramFile.isDirectory()) {
                File[] arrayOfFile = paramFile.listFiles();
                for (int i = 0; i < arrayOfFile.length; i++) {
                    deleteFile(arrayOfFile[i]);
                }
            }
        }
    }

    public static void deleteFile(String paramString) {
        deleteFile(new File(paramString));
    }

    public static boolean isSdCardExist(){
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    public static String  getDocumentPath() {
        boolean flag = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        File localFile = null;
        if (flag) {
            localFile = Environment.getExternalStorageDirectory();
        }
        return localFile.toString();
    }

    public static String getLocalFileRootPath() {
        checkDir(FILE_PATH+"/files");
        return getDocumentPath() + "/"+FILE_PATH+"/files";
    }

    public static String getLocalLogPath() {
        checkDir(FILE_PATH+"/logs");
        return getDocumentPath() + "/"+FILE_PATH+"/logs";
    }

    public static String getLocalFileCrashPath() {
        checkDir(FILE_PATH+"/files/crash");
        return getDocumentPath() + "/"+FILE_PATH+"/files/crash";
    }

    public static String getLocalFileTempPath() {
        checkDir(FILE_PATH+"/files/temp");
        return getDocumentPath() + "/"+FILE_PATH+"/files/temp";
    }

    public static String getLocalFileCachePath() {
        checkDir(FILE_PATH+"/files/cache");
        return getDocumentPath() + "/"+FILE_PATH+"/files/cache";
    }

    public static String getLocalFileVoicePath() {
        checkDir(FILE_PATH+"/files/voice");
        return getDocumentPath() + "/"+FILE_PATH+"/files/voice";
    }

    public static String getLocalFileImagePath() {
        checkDir(FILE_PATH+"/files/image");
        return getDocumentPath() + "/"+FILE_PATH+"/files/image/";
    }

    public static String getCameraPath(){
        return getDocumentPath()+"/DCIM/Camera";
    }

    public static boolean isDirExist(String paramString) {
        return new File(getDocumentPath() + "/" + paramString).exists();
    }

    public static synchronized String createFileName() {
        java.util.Date dt = new java.util.Date(System.currentTimeMillis());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileName = fmt.format(dt);
        cnt = (cnt + 1) % 100;
        StringBuffer sBuf = new StringBuffer("0000").append(cnt);
        sBuf.delete(0, sBuf.length() - 2);
        fileName = fileName + "_" + sBuf.toString();
        return fileName;

    }

    public static long sizeOfDirectory(File directory) {
        String var6;
        if(!directory.exists()) {
            var6 = directory + " does not exist";
            throw new IllegalArgumentException(var6);
        } else if(!directory.isDirectory()) {
            var6 = directory + " is not a directory";
            throw new IllegalArgumentException(var6);
        } else {
            long size = 0L;
            File[] files = directory.listFiles();
            if(files == null) {
                return 0L;
            } else {
                for(int i = 0; i < files.length; ++i) {
                    File file = files[i];
                    if(file.isDirectory()) {
                        size += sizeOfDirectory(file);
                    } else {
                        size += file.length();
                    }
                }

                return size;
            }
        }
    }

    public static void cleanDirectory(File directory) throws IOException {
        String var7;
        if(!directory.exists()) {
            var7 = directory + " does not exist";
            throw new IllegalArgumentException(var7);
        } else if(!directory.isDirectory()) {
            var7 = directory + " is not a directory";
            throw new IllegalArgumentException(var7);
        } else {
            File[] files = directory.listFiles();
            if(files == null) {
                throw new IOException("Failed to list contents of " + directory);
            } else {
                IOException exception = null;

                for(int i = 0; i < files.length; ++i) {
                    File file = files[i];

                    try {
                        forceDelete(file);
                    } catch (IOException var6) {
                        exception = var6;
                    }
                }

                if(exception != null) {
                    throw exception;
                }
            }
        }
    }

    public static void deleteDirectory(File directory) throws IOException {
        if(directory.exists()) {
            cleanDirectory(directory);
            if(!directory.delete()) {
                String message = "Unable to delete directory " + directory + ".";
                throw new IOException(message);
            }
        }
    }

    public static void forceDelete(File file) throws IOException {
        if(file.isDirectory()) {
            deleteDirectory(file);
        } else {
            boolean filePresent = file.exists();
            if(!file.delete()) {
                if(!filePresent) {
                    throw new FileNotFoundException("File does not exist: " + file);
                }

                String message = "Unable to delete file: " + file;
                throw new IOException(message);
            }
        }

    }

    public static boolean deleteQuietly(File file) {
        if(file == null) {
            return false;
        } else {
            try {
                if(file.isDirectory()) {
                    cleanDirectory(file);
                }
            } catch (Exception var3) {
                ;
            }

            try {
                return file.delete();
            } catch (Exception var2) {
                return false;
            }
        }
    }
}
